package net.atomarrow.util.excel;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;

import net.atomarrow.util.BeanUtil;

public abstract class BaseData  implements ExcelDataInterface {

	protected int row;
	protected int column;
	protected CellStyle CellStyle;
	
	public BaseData(int row, int column, CellStyle CellStyle) {
		this.row=row;
		this.column=column;
		this.CellStyle=CellStyle;
	}

	@Override
	/**
	 * 自动处理位置
	 */
	public void ReplaceAuto(int row, int column) {
		
		switch(this.row){
		case AUTO:
			//if use AUTO ,auto incream the location
			this.row=row+1;
			break;
		case LAST:
			// if use LAST, keep the location at the last location
			this.row=row;
		default:
			if(Math.abs(this.row-BASE_LAST)<=1000){
				this.row=row+this.row-BASE_LAST;
			}
			break;
		}
		
		switch(this.column){
		case AUTO:
			this.column=column+1;
			break;
		case LAST:
			this.column=column;
			break;
		default:
			if(Math.abs(this.column-BASE_LAST)<=1000){
				this.column=column+this.column-BASE_LAST;
			}
		}
	}


	public CellStyle getCellStyle() {
		return CellStyle;
	}
	/**
	 * 合并单元格
	 * @param regions
	 */
	protected void processRegions(List<CellRangeAddress> regions){
		if(regions==null)return;
		for(CellRangeAddress region:regions){
			processRegion(region);
		}
	}

	private void processRegion(CellRangeAddress region) {
		if(region==null)return;
		if(row==region.getFirstRow()&&column==region.getFirstColumn())return;
		if(row>=region.getFirstRow()&&row<=region.getLastRow()
				&&column>=region.getFirstColumn()&&column<=region.getLastColumn()
				){
			column=region.getLastColumn();
		}
	}
	/**
	 * 判断是否需要用户处理
	 */
	protected Object changeValueIfNecessary(String fieldName,Object value,ExcelFormatListener format){
		Object result;
		if(format!=null){
			result=format.changeValue(fieldName, value, row, column);
			if(result!=null){
				if(result instanceof Throwable){
					((Throwable)result).printStackTrace();
				}else{
					value=result;
				}
			}
		}
		return value;
	}
	/**
	 * 把单个实体转换为各个单元格
	 * @param regions  单元格合并信息
	 * @param cellDatas  单元格集合
	 * @param columnMap  列map
	 * @param data      实体对象
	 * @param columns   对应列
	 * @param index     对应下标
	 * @param format    回调函数
	 */
	protected void addDomainToList(List<CellRangeAddress> regions, List<CellData> cellDatas,Map columnMap,Object data,String[] columns,int index,ExcelFormatListener format,ExcelDataChangeListener dataChanger){
		Field field;
		Object value = null;
			Class clazz = data.getClass();
			for (String fieldName : columns) {
				value=changeDataIfNecessary(fieldName,data,dataChanger);
				if(value==null){
					if(columnMap!=null){
						value=columnMap.get(fieldName);
						if(value!=null){
							if(value instanceof List){
								// process list
								value=((List)value).get(index);
							}else if(value instanceof Object[]){
								// porcess arrays
								value =((Object[])value)[index];
							}
						}
					}
				}
				// if value not find in columnMap,then find it in object
				if(value==null){
						//first find the get Method and get the value
					value = BeanUtil.getSimpleProperty(data,fieldName);
				}
				processRegions(regions);
				
				value=changeValueIfNecessary(fieldName, value, format);
				cellDatas.add(new CellData(row,column++,value==null?"":value.toString(),getCellStyle()));
			}
	}

	private Object changeDataIfNecessary(String fieldName, Object data,
			ExcelDataChangeListener dataChanger) {
		Object result = null;
		if(dataChanger!=null){
			result=dataChanger.changeValue(fieldName, data, row, column);
			if(result!=null){
				if(result instanceof Throwable){
					((Throwable)result).printStackTrace();
				}
			}
		}
		return result;
	}
}
